const {ccclass, property} = cc._decorator;

import {initMgr} from './initMgr';
import parseUrl from './urlParse';

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.Label)
    label: cc.Label = null;

    @property(cc.Label)
    loadingProgess: cc.Label = null;

    _mainScene: string;

    _splash: cc.Node;
    

    onLoad() {
        
        if(!cc.sys.isNative && cc.sys.isMobile){
            let cvs = this.node.getComponent(cc.Canvas);
            cvs.fitWidth = true;
            cvs.fitHeight = true;
        }
        initMgr();
        
        
        this._mainScene = 'loading';
        this.showSplash(() => {
            /**
             * 版本文件路径
             */
            const url = cc.url.raw('resources/ver/cv.txt');
            cc.loader.load(url, (err, data) => {
                cc.VERSION = data;
                console.log('当前的客户端游戏版本号：' + cc.VERSION);
                this.getServerInfo();
            });
        });
    }

    /**
     * 控制LOGO显示3秒后用0.5秒时间渐渐隐藏后调用回调切换画面
     * @param callback 
     */
    showSplash(callback){
        this._splash = cc.find('Canvas/splash');
        this._splash.active = true;
        if(this._splash.getComponent(cc.Sprite).spriteFrame == null){
            callback();
            return;
        }
        const SHOW_TIME = 3000;
        const FADE_TIME = 500;
        let t = Date.now();
        const fn = () => {
            let dt = Date.now() - t;
            if(dt < SHOW_TIME){
                setTimeout(fn, 33);
            }
            else{
                let op = (1 - ((dt - SHOW_TIME) / FADE_TIME)) * 255;
                if(op < 0){
                    this._splash.opacity = 0;
                    callback();
                }
                else{
                    this._splash.opacity = op;
                    setTimeout(fn, 33);
                }
            }
        };
        setTimeout(fn, 33);
    }

    /**
     * 请求后台版本号与客户端版本号进行对比，看是否需要更新还是进入下一场景
     */
    getServerInfo(){
        const self = this;
        const EACH_CONNECTION_TIME = 5000;
        const onGetVersion = (ret: cc.ret) => {
            if(ret.version == null){
                console.log('服务端没有返回版本号字段！');
            }
            else{
                cc.vv.SI = ret;
                if(ret.version != cc.VERSION){
                    cc.find('Canvas/alert').active = true;
                }
                else{
                    cc.director.loadScene(self._mainScene);
                }
            }
        };

        let xhr:XMLHttpRequest = null;
        let complete = false;
        const fnRequest = () => {
            self.loadingProgess.string = '正在连接服务器';
            xhr = cc.vv.http.sendRequest('/get_serverinfo', null, (ret) => {
                xhr = null;
                complete = true;
                onGetVersion(ret);
            });
            setTimeout(fn, EACH_CONNECTION_TIME);
        };

        const fn = () => {
            if(!complete){
                if(xhr) {
                    xhr.abort();
                    self.loadingProgess.string = '连接失败，即将重试！';
                    setTimeout(() => {
                        fnRequest();
                    }, EACH_CONNECTION_TIME);
                }
                else {
                    fnRequest();
                }
            }
        };
        fn();
    }

    /**
     * 打开游戏下载页面
     */
    onBtnDownloadClicked(){
        cc.sys.openURL(cc.vv.SI.appweb);
    }
}
